#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>;
#include<stdbool.h>

typedef struct ListNode
{
	int val;
	struct ListNode* next;
}ListNode;

typedef struct MyCirQue
{
	ListNode* front;
	ListNode* prev;
	ListNode* tail;
	int size;
}MyCirQue;

MyCirQue* MyCirQueInit(int k)
{
	MyCirQue* ps = (MyCirQue*)malloc(sizeof(MyCirQue));
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	newnode->next = NULL;
	ps->prev = NULL;
	ps->front = ps->tail = newnode;
	ps->size = 0;
	while (--k)
	{
		ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
		newnode->next = NULL;
		ps->tail->next = newnode;
		ps->tail = ps->tail->next;
		ps->size++;
	}
	ps->tail->next = ps->front;
	ps->tail = ps->front;
}

bool MyCirQueIsEmpty(MyCirQue* ps)
{
	return ps->front == ps->tail;
}

bool MyCirQueIsFull(MyCirQue* ps)
{
	return ps->tail->next == ps->front;
}

bool MyCirQuePush(MyCirQue* ps, int x)
{
	if (MyCirQueIsFull(ps))
	{
		return false;
	}
	else
	{
		ps->tail->val = x;
		ps->prev = ps->tail;
		ps->tail = ps->tail->next;
		return true;
	}
}

bool MyCirQuePop(MyCirQue* ps)
{
	if (MyCirQueIsEmpty(ps))
	{
		return false;
	}
	else
	{
		ps->front = ps->front->next;
		return true;
	}
}

int MyCirQueFront(MyCirQue* ps)
{
	if (MyCirQueIsEmpty(ps))
	{
		return -100;
	}
	else
	{
		return ps->front->val;
	}
}

int MyCirQueTail(MyCirQue* ps)
{
	if (MyCirQueIsEmpty(ps))
	{
		return -100;
	}
	else
	{
		return ps->prev->val;
	}
}

void MyCirQuePrint(MyCirQue* ps)
{
	ListNode* cur = ps->front;
	while (cur != ps->tail)
	{
		printf("%d->", cur->val);
		cur = cur->next;
	}
	printf("NULL\n");
}

void SListDestory(MyCirQue* ps)
{
	ListNode* cur = ps->front;
	while (ps->size--)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
}

void MyCirQueDestory(MyCirQue* ps)
{
	SListDestory(ps);
	free(ps);
}

int main()
{
	MyCirQue* ps = MyCirQueInit(5);
	MyCirQuePush(ps, 1);
	MyCirQuePush(ps, 2);
	MyCirQuePush(ps, 3);
	MyCirQuePush(ps, 4);
	MyCirQuePop(ps);
	MyCirQuePrint(ps);
	MyCirQueDestory(ps);

	return 0;
}